<!-- 这个文件的内容改到vuex文件的hg3000socket中完成实现 -->
<template>

</template>

<script>
import { mapState } from 'vuex'
import {formatDate}  from "@/utils/drawdat/date"
// import { fa } from 'element-plus/es/locale';

// let  socket =  null

export default {
    data() {
      return {
        clientid: 0,
        socket: null,

      }
    },

    computed: mapState([
    'componentData',
  ]),    

    created(){
      if (typeof (WebSocket) == "undefined") {
        return;
      }
//      this.createSocket(  )
    },

    unmounted() {
    if( this.socket != null )
      this.socket.close()
      //console.log( "unmounted win "  )
    },

    methods: {
      createSocket(  bGetData = false ){
        var now =  new Date(  )
        this.clientid = formatDate( now, "yyyyMMdd-hhmmss-SSS" )
        console.log( "clientid:",  this.clientid ) 

        if( this.socket != null )
          this.socket.close()

        this.socket = new WebSocket( 'ws://localhost:9191/getData/' + this.clientid )

        const vm = this;

        this.socket.onopen = function () {
          console.log("websocket已打开");
          if( bGetData )
            vm.callData( )
        };

        //  浏览器端收消息，获得从服务端发送过来的文本消息
        this.socket.onmessage = function (msg) {
          // console.log(  msg.data )

          const msgarray  = JSON.parse( msg.data )
          for(var i=0; i<msgarray.length; i++){
              let obj = msgarray[i]
              // console.log("obj", i,   obj )
              for( var j=0; j<obj.array.length; j++){
                // console.log( vm.componentData, obj.indexid,  obj.objid, obj.array[j].value )
                //if( typeof obj.array[j].value === 'number' )
                  vm.$store.commit( 'setValue', {componentData : vm.componentData, indexid: obj.indexid,  objid: obj.objid,  fValue: obj.array[j].value } )
              }
          }

        };
        //关闭事件
        this.socket.onclose = function () {
          console.log("websocket已关闭");
        };
        //发生了错误事件
        this.socket.onerror = function () {
          console.log("websocket发生了错误");
        };        

      },

      callData( ){
        let requestarray = []
        for( var i=0; i<this.componentData.length; i++){
          const obj = this.componentData[i]

          //一个对象中有多个编号
          var datarray = []

          if( Object.keys(obj.hg3000.linkid).length > 0 ){
            datarray.push( obj.hg3000.linkid )
          }

          if( Object.keys(obj.hg3000.linkid).length > 0 ){
            for (let j = 0; j < obj.hg3000.linkids.length; j++) {
                datarray.push( obj.hg3000.linkids[j] );
            }
          }

          if( datarray.length > 0 ){
            //数组中的对象信息包括  对象编号  对象序号  该对象的数据点数组
            let datobj = {  objid: obj.id,  indexid: i,  array: datarray }    //objid 为对象的编号 
            requestarray.push( datobj )
          }
        }  //end for

        console.log( "call", requestarray )

        //发送时的信息： 客户编号  与 对象信息数组
        let sendobj = {clientid: this.clientid, callfunc: "calldata",  array: requestarray }

        //发送
        this.socket.send( JSON.stringify( sendobj ) )
      },

      sayHello(  ){
        console.log("hello")
      }
    },
}
</script>

<style lang="scss">

</style>
